using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;

public partial class UserControls_ProductListForSearch : System.Web.UI.UserControl
{
    protected DataTable Data
    {
        get
        {
            return (DataTable)Session["Data"];
        }
        set
        {
            Session["Data"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateControls();
        }
    }
    private void PopulateControls()
    {
        // Retrieve DepartmentID from the query string
        string departmentId = Request.QueryString["DepartmentID"];
        // Retrieve CategoryID from the query string
        string categoryId = Request.QueryString["CategoryID"];
        // Retrieve Page from the query string
        string page = Request.QueryString["Page"];
        if (page == null) page = "1";
        // Retrieve Search string from query string
        string searchString = Request.QueryString["Search"];
        string sortExpresion = ddlSortBox.Text.Split(' ')[0];
        string sortDirection = ddlSortBox.Text.Split(' ')[1];
        // How many pages of products?
        int howManyPages = 1;
        // If performing a product search
        if (searchString != null)
        {
            // Retrieve AllWords from query string
            string allWords = Request.QueryString["AllWords"];
            // Perform search
            Data = CatalogAccess.Search(searchString, allWords,
            page, out howManyPages, sortExpresion, sortDirection);
            list.DataSource = Data;
            list.DataBind();
        }
        else if (searchString == null)
        { }
        // If browsing a category...
        if (categoryId != null)
        {
            // Retrieve list of products in a category
            Data = CatalogAccess.GetProductsInCategory(categoryId, page, out howManyPages, sortExpresion, sortDirection);
            list.DataSource = Data;
            list.DataBind();
        }
       
        else if (departmentId != null)
        {
            // Retrieve list of products on department promotion
            Data = CatalogAccess.GetProductsOnDepartmentPromotion(departmentId, page, out howManyPages, sortExpresion, sortDirection);
            list.DataSource = Data;
            list.DataBind();
        }
        /*
        else
        {
            // Retrieve list of products on catalog promotion
            list.DataSource = CatalogAccess.GetProductsOnCatalogPromotion(page, out howManyPages);
            list.DataBind();
        }*/ 
        // display paging controls 
        // display paging controls 
        if (howManyPages > 1)
        {
            // have the current page as integer
            int currentPage = Int32.Parse(page);
            // make controls visible
            pagingLabel.Visible = true;
            previousLink.Visible = true;
            nextLink.Visible = true;

            pagingLabel1.Visible = true;
            previousLink1.Visible = true;
            nextLink1.Visible = true;

            // set the paging text
            pagingLabel.Text = "Strona " + page + " z " + howManyPages.ToString();
            pagingLabel1.Text = "Strona " + page + " z " + howManyPages.ToString();
            // create the Previous link
            if (currentPage == 1)
            {
                previousLink.Enabled = false;
                previousLink.Visible = false;

                previousLink1.Enabled = false;
                previousLink1.Visible = false;
            }
            else
            {
                NameValueCollection query = Request.QueryString;
                string paramName, newQueryString = "?";
                for (int i = 0; i < query.Count; i++)
                    if (query.AllKeys[i] != null)
                        if ((paramName = query.AllKeys[i].ToString()).ToUpper() != "PAGE")
                            newQueryString += paramName + "=" + query[i] + "&";
                previousLink.NavigateUrl = Request.Url.AbsolutePath + newQueryString
                + "Page=" + (currentPage - 1).ToString();

                previousLink1.NavigateUrl = Request.Url.AbsolutePath + newQueryString
                + "Page=" + (currentPage - 1).ToString();
            }
            // create the Next link
            if (currentPage == howManyPages)
            {
                nextLink.Enabled = false;
                nextLink.Visible = false;

                nextLink1.Enabled = false;
                nextLink1.Visible = false;
            }
            else
            {
                NameValueCollection query = Request.QueryString;
                string paramName, newQueryString = "?";
                for (int i = 0; i < query.Count; i++)
                    if (query.AllKeys[i] != null)
                        if ((paramName = query.AllKeys[i].ToString()).ToUpper() != "PAGE")
                            newQueryString += paramName + "=" + query[i] + "&";
                nextLink.NavigateUrl = Request.Url.AbsolutePath + newQueryString +
                "Page=" + (currentPage + 1).ToString();

                nextLink1.NavigateUrl = Request.Url.AbsolutePath + newQueryString +
                "Page=" + (currentPage + 1).ToString();
            }
        }
    }
    /*

    protected void list_ItemCommand(object source, DataListCommandEventArgs e)
    {
        // The CommandArgument of the clicked Button contains the ProductID
        string productId = e.CommandArgument.ToString();
        // Add the product to the shopping cart
        ShoppingCartAccess.AddItem(productId);
    }
     */
    protected void ddlSortBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Data != null)
        {
            DataView dw = new DataView(Data);
            //dw.Sort = ddlSortBox.Text;
            list.DataSource = dw;//.Select(null, ddlSortBox.Text);
            list.DataBind();
        }
    }
}




